<?php
/**
 * YOS.
 * 4/1/24
 * @Author deepsea
 * @Contact telegram:deepsea159 
 */

namespace App\Fastapp\Model;

trait FileTrait
{
    public function formatPathSet(string $field, string|array|null $v): void
    {
        if (!$v) return;
        [, , $key] = get_filesystem();
        if (is_array($v)) {
            $f = [];
            foreach ($v as $path) {
                if (is_array($path)) {
                    if (!empty($path['text'])) {
                        $p = parse_url($path['text']);
                        $path['text'] = $key == 'local' ? str_replace('/' . env('UPLOAD_PATH', 'uploadfile'), '', $p['path']) : $p['path'];
                        $f[] = $path;
                    }
                } else {
                    $p = parse_url($path);
                    $f[] = $key == 'local' ? str_replace('/' . env('UPLOAD_PATH', 'uploadfile'), '', $p['path']) : $p['path'];
                }
            }
            $this->attributes[$field] = json_encode($f);
        } else {
            $p = parse_url($v);
            $this->attributes[$field] = $key == 'local' ? str_replace('/' . env('UPLOAD_PATH', 'uploadfile'), '', $p['path']) : $p['path'];
        }
    }

    public function formatPathGet(string|array|null $v): array|string
    {
        $f = '';
        if ($v) $f = json_decode($v, true);
        if (is_array($f)) {
            foreach ($f as $k => $path) {
                if (is_array($path)) {
                    if (!empty($path['text'])) {
                        $path['text'] = get_file_url($path['text']);
                        $f[$k] = $path;
                    }
                } else $f[$k] = get_file_url($path);
            }
            return $f;
        }
        if ($v) return get_file_url($v);
        return '';
    }

    public function getImageAttribute($value): array|string
    {
        return $this->formatPathGet($value);
    }

    public function setImageAttribute($value): void
    {
        $this->formatPathSet('image', $value);
    }

    public function getCoverAttribute($value): array|string
    {
        return $this->formatPathGet($value);
    }

    public function setCoverAttribute($value): void
    {
        $this->formatPathSet('cover', $value);
    }

    public function getIconAttribute($value): string
    {
        return $this->formatPathGet($value);
    }

    public function setIconAttribute($value): void
    {
        $this->formatPathSet('icon', $value);
    }

    public function getLogoAttribute($value): string
    {
        return $this->formatPathGet($value);
    }

    public function setLogoAttribute($value): void
    {
        $this->formatPathSet('logo', $value);
    }

    public function getAvatarAttribute($value): string
    {
        return $this->formatPathGet($value);
    }

    public function setAvatarAttribute($value): void
    {
        $this->formatPathSet('avatar', $value);
    }

    public function getImagesAttribute($value): array
    {
        return $this->formatPathGet($value) ?: [];
    }

    public function setImagesAttribute($value): void
    {
        $this->formatPathSet('images', $value);
    }
}